package cn.csm.jwtlogin.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.Collections;

/**
 * description: OpenApiConfig <br>
 * 访问地址：http://localhost:8080/swagger-ui.html
 * date: 2020/8/24 9:55 <br>
 * author: shimi <br>
 * version: 1.0 <br>
 */
@Configuration
public class OpenApiConfig {

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .openapi("3.0.0")
                .info(new Info()
                        .title("jwt-login")
                        .version("1.0")
                        .description("jwt 登录demo"))
                .servers(
                        Arrays.asList(
                                new Server().url("http://localhost:8080").description("开发环境"),
                                new Server().url("http://192.168.0.1:8080").description("生产环境")
                        )
                )
                //配置全局认证方式
                .components(new Components()
                        .addSecuritySchemes("bearerAuth",
                                new SecurityScheme().
                                        type(SecurityScheme.Type.HTTP)
                                        .scheme("bearer")
                                        .bearerFormat("JWT"))
                )
                // 全局认证
                .security(Collections.singletonList(new SecurityRequirement().addList("bearerAuth")));
    }


    @Bean
    public GroupedOpenApi resourceOpenApi() {
        String[] packagesToscan = {"cn.csm.jwtlogin"};
        return GroupedOpenApi.builder().group("接口文档").packagesToScan(packagesToscan)
                .build();
    }

}
